/**
 *author:yhb
 */
package com.towker.frame.controller.rbac;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.util.BeanUtils;
import com.towker.frame.service.rbac.*;
import com.towker.frame.share.rbac.res.PermissionShare;
import com.towker.frame.share.sys.user.UserShare;
import com.towker.web.MyRequest;

@Controller
@RequestMapping("/manage/rbac")
public class RoleResourceController
{
	//角色资源关系业务对象。
	private RoleResourceService service;
	public void setRoleResourceService(RoleResourceService service){this.service = service;}
	/**
	 * 给角色分配资源。*/	@RequestMapping("/assignRoleRes{viewName}")
	public ModelAndView addRoleRes(HttpServletRequest request, HttpServletResponse response,@PathVariable String viewName) throws IOException
	{
		ModelAndView result=null;
		if("1".equals(viewName)){
			result = new ModelAndView();
			result.setViewName("/manage/rbac/assignRoleRes.jsp");
		}else if("2".equals(viewName)){
			PrintWriter out = response.getWriter();
			MyRequest req = new MyRequest(request);
			long sysid = req.getLong("sysid");
			long roleid = req.getLong("roleid");
			String resIds = req.getString("resIds");
			try{
				service.assignRoleRes(sysid, roleid, resIds);
				PermissionShare permissionHelper=(PermissionShare)BeanUtils.getBean(PermissionShare.class);
				permissionHelper.removeUserPermissionCache(UserShare.getCurrentUserId(request)+"");
				permissionHelper.removeSysPermissionCache(sysid+"");
				out.print(1);
			}catch (Exception e){e.printStackTrace();out.print(0);}
		}
		return result;
	}
}
